package com.hb.znpt.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;

import com.hb.znpt.BR;
import com.hb.znpt.R;
import com.hb.znpt.bean.UpgradeBean;
import com.hb.znpt.databinding.ActivityMainBinding;
import com.hb.znpt.utils.AssestUtils;
import com.hb.znpt.utils.Constants;
import com.hb.znpt.utils.OssManager;
import com.hb.znpt.viewmodel.MainViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.unitlib.base.base.BaseActivity;
import com.unitlib.base.utils.SPUtils;
import com.unitlib.base.utils.constant.LiveConstants;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        new AssestUtils(this).init();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssManager.getInstance().init(context);
            }
        }).start();
    }

    @Override
    public void initViewObservable() {
        viewModel.quitData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
                normalDialog.setMessage("是否要退出登录?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SPUtils.getInstance().put(Constants.SP_LOGINSTATUS, false);
                                SPUtils.getInstance().put(Constants.SP_USERCARD, "");
                                startActivity(LoginActivity.class);
                                finish();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
            }
        });
        viewModel.updataData.observe(this, new Observer<UpgradeBean>() {
            @Override
            public void onChanged(UpgradeBean bean) {
                showUpdataDialog(bean);
            }
        });
    }

    public void showUpdataDialog(UpgradeBean bean) {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
        normalDialog.setMessage("有新的安装包，是否更新?")
                .setTitle("更新提示")
                .setPositiveButton("现在更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        ProgressDialog mpDialog = new ProgressDialog(context);
                        mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        mpDialog.setMessage("已下载");
                        mpDialog.setCancelable(false);
                        mpDialog.setCanceledOnTouchOutside(false);

                        viewModel.download(bean);
                    }
                })
                .setNegativeButton("稍后更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

    }

    @Override
    public void initLiveEventBus() {
        LiveEventBus.get(LiveConstants.LIVEDATABUS_KEY_GLOBAL_FINISH, Integer.class)
                .observe(this, new Observer<Integer>() {
                    @Override
                    public void onChanged(Integer integer) {
                        SPUtils.getInstance().put(Constants.SP_LOGINSTATUS, false);
                        SPUtils.getInstance().put(Constants.SP_USERCARD, "");
                        startActivity(LoginActivity.class);
                        finish();
                    }
                });
    }
}